package com.insist.controller;

import com.insist.model.RewardModel;
import com.insist.service.RewardService;
import com.insist.util.CmmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/6.
 */
@Controller
@RequestMapping("/reward")
public class RewardController {

    @Autowired
    RewardService rewardService;

    /**
     * 查询最近的中奖信息（5条）
     * @return
     */
    @RequestMapping("/rewardList")
    @ResponseBody
    public List<RewardModel> rewardList(){
        List<RewardModel> infos = rewardService.findRewardList();
        for(RewardModel info:infos){
            info.setProductImage(CmmUtils.replacePictureUrl(info.getProductImage()));
        };
        return infos;
    }

    @RequestMapping("/productReward")
    @ResponseBody
    public List<RewardModel> findProductReward(Long productId){
        List<RewardModel> infos = rewardService.findProductReward(productId);
        for(RewardModel info:infos){
            info.setProductImage(CmmUtils.replacePictureUrl(info.getProductImage()));
        };
        return infos;
    }

    @RequestMapping("/userReward")
    @ResponseBody
    public List<RewardModel> findUserReward(){
        if(CmmUtils.getLoginUserId()==null){
            return new ArrayList<>();
        }
        List<RewardModel> infos = rewardService.findUserReward(CmmUtils.getLoginUserId());
        for(RewardModel info:infos){
            info.setProductImage(CmmUtils.replacePictureUrl(info.getProductImage()));
        };
        return infos;
    }



}
